package com.pangolin.pangolinim.ui.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.SPUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseActivity {
    private Button mBtnLogin;
    private ImageButton mIbAddMenu;
    private EditText mEtPwd;
    private EditText mEtUid;
    private View mVLinePwd;
    private View mVLineUid;

    private int mUid;        //用于临时存放请求中的用户uid
    private String mPasswd;  //用于临时存放请求中的用户passwd

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mBtnLogin.setEnabled(canLogin());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    @Override
    protected void init(){
    }

    @Override
    protected void initView(){
        mBtnLogin = findViewById(R.id.btnLogin);
        mIbAddMenu =  findViewById(R.id.ibAddMenu);
        mVLinePwd = findViewById(R.id.vLinePwd);
        mVLineUid = findViewById(R.id.vLineUid);
        mEtUid = findViewById(R.id.etUid);
        mEtPwd = findViewById(R.id.etPwd);

        setToolbarTitle("登陆");
        mIbAddMenu.setVisibility(View.GONE);
    }

    @Override
    protected void initListener(){
        mEtPwd.addTextChangedListener(watcher);
        mEtUid.addTextChangedListener(watcher);
        mEtPwd.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mVLinePwd.setBackgroundColor(UIUtil.getColor(R.color.green0));
            } else {
                mVLinePwd.setBackgroundColor(UIUtil.getColor(R.color.line));
            }
        });
        mEtUid.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mVLineUid.setBackgroundColor(UIUtil.getColor(R.color.green0));
            } else {
                mVLineUid.setBackgroundColor(UIUtil.getColor(R.color.line));
            }
        });

        mBtnLogin.setOnClickListener(v -> {
            login();

        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA){
            if(bundleUtil.getData() != null){
                Map<String, Object> resMap = bundleUtil.getData();
                Logger.d(resMap);
                if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_LOGIN){       //登陆请求返回
                    if((int)resMap.get("result") == 0){
                        UIUtil.showToast("自动登陆成功");
                        //将用户密码缓存，以便此后自动登陆
                        SPUtil.put(this , "uid" , mUid);
                        SPUtil.put(this , "passwd" , mPasswd);
                        //Logger.d("putSharedPreferences : %d|%s" , mUid , mPasswd);

                        UIContact me = new UIContact();
                        me.setUid(mUid);
                        me.setName((String) resMap.get("name"));
                        me.setHeader((String) resMap.get("headimg"));
                        jumpToActivity(MainActivity.class , GsonUtil.toJson(me));
                        finish();
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_login;
    }

    private boolean canLogin() {
        int pwdLength = mEtPwd.getText().toString().trim().length();
        int phoneLength = mEtUid.getText().toString().trim().length();
        if (pwdLength > 0 && phoneLength > 0) {
            return true;
        }
        return false;
    }

    private void login(){
        int uid = Integer.parseInt(mEtUid.getText().toString());
        String passwd = mEtPwd.getText().toString();
        if(uid <= 0 || mEtUid.getText().toString().trim().length() <= 0)
            return;
        mUid = uid;
        mPasswd = passwd;
        Map<String, Object> brocastMap = new HashMap<String, Object>();
        brocastMap.put("type" , BroadcastMsg.DATA_UI_LOGIN);
        brocastMap.put("uid" , uid);
        brocastMap.put("passwd" , passwd);
        BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
        BroadcastSender(bundleUtil.getBundle());
    }
}